<?php
$oJobgroups = JobgroupHandler::getJobgroups();
$oJobs = JobHandler::getJobs();

$iGroupedJobs = array();
foreach ($oJobgroups as $oThisJobgroup)
{
	$iJobs = $oThisJobgroup->getJobs();
	foreach ($iJobs as $iThisJob)
	{
		$iGroupedJobs[$iThisJob] = $oThisJobgroup->iID;
	}
}
?>

<script language="JavaScript">
	function setDefaultJob(iJob)
	{
		location.href = "index.php?sView=job-list&sVar=defaultjob&mValue=" + iJob;
	}
</script>
<table class="boxborder" cellspacing="0">
	<tr>
		<td class="boxhandle" colspan="6"><?php print msg('Projects') ?></td>
	</tr>
	<tr>
		<td class="control small" colspan="6" nowrap>
			<?php print msg('Projects') ?> | <a href="index.php?sView=job-jobgrouplist"><?php print msg('Projectgroups') ?></a> |
		</td>
	</tr>
	<tr>
		<td class="listtop center nowrap"><a href="javascript: showWindow('job-add');"><img src="images/<?php print $_SESSION['oUserVars']['images'] ?>/job-add.png" border="0"></a></td>
		<td class="listtop" nowrap><?php print msg('Project name') ?></td>
		<td class="listtop center nowrap"><?php print msg('Status') ?></td>
		<td class="listtop center nowrap"><?php print msg('Owner') ?></td>
		<td class="listtop"><?php print msg('Creation') ?></td>
		<td class="listtop center"><?php print msg('Main') ?></td>
	</tr>
<?php

foreach ($oJobgroups as $oThisJobgroup) {
	$iJobs = $oThisJobgroup->getJobs();
	?>
	<tr>
		<td class="separator" colspan="6"><?php print $oThisJobgroup->sName ?></td>
	</tr>
	<?php
	foreach ($iJobs as $iThisJob) {
		if (is_object($oJobs[$iThisJob])) {
			showJob($oJobs[$iThisJob]);
		}
	}
}

if (count($oJobgroups) > 0 && (count($oJobs) != count($iGroupedJobs)))
{
	?>
	<tr>
		<td class="separator" colspan="6"><?php print msg('Ungrouped projects') ?></td>
	</tr>
	<?php
}

foreach ($oJobs as $oThisJob)
{
	if (!@$iGroupedJobs[$oThisJob->iID])
	{
		showJob($oThisJob);
	}
}

function showJob($oThisJob)
{
	$iJobStatus = JobHandler::getJobStatus($oThisJob->iID);
	$oUsers = UserHandler::getUsers();
	?>
	<tr>
		<td class="listentity center small" nowrap>
	<?php
	if (JobHandler::canDo('job', 'write', $oThisJob->iID))
	{
		print '<a href="javascript: showWindow(\'job-delete\', { iJob: '.$oThisJob->iID.' });"><img src="images/'.$_SESSION['oUserVars']['images'].'/job-delete.png" border="0"></a> ';
	}
	else
	{
		print '<img src="images/blank.gif" width="'.$_SESSION['oUserVars']['images'].'"> ';
	}
	print '<a href="javascript: showWindow(\'job-add\', { iJob: '.$oThisJob->iID.'});"><img src="images/'.$_SESSION['oUserVars']['images'].'/job-change.png" border="0"></a> ';
	?>
		</td>
		<td class="listentity" nowrap>
			<a href="index.php?sView=job-view&iJobID=<?php print $oThisJob->iID ?>"><?php print $oThisJob->sName ?></a>
	<?php
	switch ($oThisJob->iIssueCount) {
		case 0:
			break;
		case 1:
			print '<span class="error small">(1 '.msg('issue').')</span>';
			break;
		default:
			print '<span class="error small">('.$oThisJob->iIssueCount.' '.msg('issues').')</span>';
			
	}
	?>
		</td>
		<td class="listentity small center nowrap">
			<table cellspacing="0" class="boxborder">
				<tr>
					<td class="missionstatus0 center small" style="width: 16px;"><?php print (@$iJobStatus[0] ? $iJobStatus[0] : '0') ?></td>
					<td class="missionstatus2 center small" style="width: 16px;"><?php print (@$iJobStatus[2] ? $iJobStatus[2] : '0') ?></td>
					<td class="missionstatus5 center small" style="width: 16px;"><?php print (@$iJobStatus[5] ? $iJobStatus[5] : '0') ?></td>
				</tr>
			</table>
		</td>
		<td class="listentity small center nowrap"><?php print ($oThisJob->iOwner ? '<a href="index.php?sView=user-view&iUser='.$oThisJob->iOwner.'">'.$oUsers[$oThisJob->iOwner]->sUser.'</a>' : '[ '.msg('Nobody').' ]') ?></td>
		<td class="listentity small nowrap"><?php print coolDate($oThisJob->iStart) ?></td>
		<td class="listentity small center">
	<?php
	if (@$_SESSION['oUserVars']['defaultjob'] == $oThisJob->iID) {
		print '<input type="radio" name="iDefaultJob" value="0" onClick="setDefaultJob(0);" checked>';
	}
	else {
		print '<input type="radio" name="iDefaultJob" value="'.$oThisJob->iID.'" onClick="setDefaultJob('.$oThisJob->iID.');">';
	}
	?>
		</td>
	</tr>
	<?php
}
?>
</table>

